package com.aya.base.job.ext;

import org.quartz.JobDetail;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

public class SpringSingletonBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {


    private ApplicationContext applicationContext;

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
//        return super.createJobInstance(bundle);
        JobDetail jobDetail = bundle.getJobDetail();
        String targetBean = jobDetail.getJobDataMap().getString("targetBean");
        Object jobBean = applicationContext.getBean(targetBean);
        return jobBean;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        this.applicationContext = applicationContext;
    }
}
